package com.netqin.netconnection;

import java.util.ArrayList;

import android.content.Context;

/**
 * MultiFileDownload singleton实例化类
 * 
 * @author MingLi
 * @since 2011-11-10
 * 
 * 
 *        使用示例 定义为成员变量 List<FileDownload> list = new ArrayList<FileDownload>(); MultiFileDownload
 *        fileDownload = null;
 * 
 *        //任务的添加 String urls[]= {"http://nokia.lt163.com/uploads/allimg/090711/121163K3K-4.png",
 *        "http://www.kofei.com/kofeipic/allimg/090503/1321263026-3.png",
 *        "http://www.web07.cn/uploads/Png/c101028/12X22WG2Z-23X94.png",
 *        "http://files.jb51.net/scimg/web/20101024/firefox_true.png",
 *        "http://www.web07.cn/uploads/Png/c101028/12X22WJ060-291514.png",
 *        "http://www.yingsheji.com/uploads/allimg/100426/095I1B45-0.png",
 *        "http://r.61658.com/www_jd/img/w/a0.png", "http://r.61658.com/www_jd/img/w/a1.png",
 *        "http://r.61658.com/www_jd/img/w/a2.png", "http://r.61658.com/www_jd/img/w/a3.png",
 *        "http://r.61658.com/www_jd/img/w/a4.png", "http://r.61658.com/www_jd/img/w/a5.png",
 *        "http://r.61658.com/www_jd/img/w/a6.png", "http://r.61658.com/www_jd/img/w/a7.png"};
 * 
 *        for (int i = 0; i < urls.length; i++) { Message msg = new Message(); msg.what =
 *        KRefreshImageMsg; list.add(fileDownload.addTask(urls[i],msg, handler)); }
 * 
 *        //任务开始 //队列级 fileDownload.start();
 * 
 * 
 *        //任务级别 FileDownload down = null; for (int i = 0; i < list.size(); i++) { down =
 *        list.get(i); down.start(); }
 */
public class FileDownloadMgrFactory {

    /**
     * 下载的类型,主要是用于和不同的缓存关联
     */
    static public final int COMMON_RESOUCE_TYPE = 0;// 通用接口
    static public final int STEADY_RESOURCE_TYPE = 1;// 不经常发生变化的或者变化周期相对较长

    /**
     * 获取文件下载实例
     * 
     * @param context
     * @param type
     *            类型，主要是用于和不同的缓存关联
     * @return
     */
    static public FileDownloadMgr getInstance(Context context, int type) { 
        if (sFileDownloadMgrList == null) {
            sFileDownloadMgrList = new ArrayList<FileDownloadMgr>();
        } 
        sFileDownloadMgrList.ensureCapacity(type + 1);
        
        for (int i = sFileDownloadMgrList.size(); i < type + 1; i++) {
            sFileDownloadMgrList.add(null);            
        }
        
        if (sFileDownloadMgrList.get(type)==null) {  
            sFileDownloadMgrList.set(type, new FileDownloadMgrImp(context));
        }
        
        return sFileDownloadMgrList.get(type);
    }

    /**
     * 通用接口，用于变化比较频繁的资源
     * @param context
     * @return
     */
    static public FileDownloadMgr getInstance(Context context) {      
        return getInstance(context,COMMON_RESOUCE_TYPE);
    }
  
    static private ArrayList<FileDownloadMgr> sFileDownloadMgrList;// 文件下载的实例数组
}